<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="170px" label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="队列分组名称" prop="group_name">
                <el-input v-model.trim="model.group_name" placeholder="请输入队列分组名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="mq配置标识" prop="mq_config">
                <el-input v-model.trim="model.mq_config" placeholder="请输入mq配置标识" clearable></el-input>
            </el-form-item>
            <el-form-item label="队列名称" prop="queue_name">
                <el-input v-model.trim="model.queue_name" placeholder="请输入队列名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="队列交换机名" prop="exchange_name">
                <el-input v-model.trim="model.exchange_name" placeholder="请输入队列交换机名" clearable></el-input>
            </el-form-item>
            <el-form-item label="队列绑定key" prop="bind_key">
                <el-input v-model.trim="model.bind_key" placeholder="请输入队列绑定key" clearable></el-input>
            </el-form-item>
            <el-form-item label="队列执行方法" prop="queue_function">
                <el-input v-model.trim="model.queue_function" placeholder="请输入队列执行方法" clearable></el-input>
            </el-form-item>
            <el-form-item label="最大进程数" prop="max_process_num">
                <el-input v-model.trim="model.max_process_num" placeholder="请输入最大进程数" clearable></el-input>
            </el-form-item>
            <el-form-item label="处理数据能力" prop="max_do_ready_num">
                <el-input v-model.trim="model.max_do_ready_num" placeholder="请输入处理数据能力" clearable></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { getDetail, edit, add } from '@/api/system/mq'

export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: '',
                group_name: '',
                mq_config: '',
                queue_name: '',
                exchange_name: '',
                bind_key: '',
                queue_function: '',
                max_process_num: '',
                max_do_ready_num: '',
            },
            rules: {
                group_name: [{ required: true, message: '队列分组名称不能为空', trigger: ['blur', 'change'] }],
                mq_config: [{ required: true, message: 'mq配置标识不能为空', trigger: ['blur', 'change'] }],
                queue_name: [{ required: true, message: '队列名称不能为空', trigger: ['blur', 'change'] }],
                exchange_name: [{ required: true, message: '队列交换机名不能为空', trigger: ['blur', 'change'] }],
                bind_key: [{ required: true, message: '队列绑定key不能为空', trigger: ['blur', 'change'] }],
                queue_function: [{ required: true, message: '队列执行方法不能为空', trigger: ['blur', 'change'] }],
                max_process_num: [{ required: true, message: '最大进程数不能为空', trigger: ['blur', 'change'] }],
                max_do_ready_num: [{ required: true, message: '处理数据能力不能为空', trigger: ['blur', 'change'] }],
            },
        }
    },
    computed: {
        title() {
            return  `${this.type === 'edit' ? '编辑' : '新增'}任务队列`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        // this.getDetail()
    },
    methods: {
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            getDetail(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                const request = this.type === 'edit' ? edit : add
                request(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally((e) => {
                        console.log(e);
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
</style>
